import type { IRoute } from 'umi';
import { dynamic } from 'umi';
import type { Settings as LayoutSettings } from '@ant-design/pro-layout';

/**
 * @see  https://umijs.org/zh-CN/plugins/plugin-initial-state
 * */
export async function getInitialState(): Promise<{
  settings?: Partial<LayoutSettings>;
  currentUser?: { [key: string]: any };
}> {
  const currentUser = {
    name: 'admin',
    role: 'editor',
    avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
    userid: '00000001',
  };
  return {
    currentUser,
    settings: {},
  };
}

function buildRoutes(routes: any) {
  return (routes || []).map((route: any) => {
    if (route.routes && route.routes.length > 0) {
      return {
        ...route,
        routes: buildRoutes(route.routes),
      };
    }

    return {
      ...route,
      component: route.component ? route.component :dynamic({
        loader: () => import('@/layouts/MicroAppLayout'),
      })
    }
  });
}
export function patchRoutes({ routes }: { routes: IRoute[] }) {
  routes[2].routes  = buildRoutes(routes[2].routes);
}

export function render(oldRender: Function) {
  oldRender();
}
